package com.example.calculationtest

import android.content.DialogInterface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.findNavController
import androidx.navigation.ui.setupActionBarWithNavController
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private lateinit var controller:NavController
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        controller = findNavController(R.id.fragment)
        setupActionBarWithNavController(controller)
    }

    override fun onSupportNavigateUp(): Boolean {
        if (controller.currentDestination?.id == R.id.questionFragment) {
            val alertDialog = AlertDialog.Builder(this)
            alertDialog.apply {
                setMessage("是否要退出")
                setPositiveButton("是") {_, _ ->
                    controller.navigate(R.id.action_questionFragment_to_titleFragment)
                }
                setNegativeButton("否", null)
                create()
                show()
            }
        }
        return super.onSupportNavigateUp()
    }

    override fun onBackPressed() {
        onSupportNavigateUp()
    }
}